package com.zhengjiang.thread;

/**
 * @author zhengjiang
 * @version 1.0.0
 * @description
 * @date 2021/6/13
 **/
public class ThreadLocalTest {

    public static void main(String[] args) {
        Account account = new Account("初始账户");
        new MyThread(account,"线程甲").start();
        new MyThread(account,"线程乙").start();
    }

}

class MyThread extends Thread{

    private Account account;

    public MyThread(Account account,String name){
        super(name);
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0;i<10;i++){
            if (i == 6){
                this.account.setName(getName());
            }
            System.out.println(account.getName() + "账户的i值:" + i);
        }
    }
}


class  Account{

    private ThreadLocal<String> name = new ThreadLocal<>();
    public Account(String name){
        this.name.set(name);
        System.out.println("====================="+this.name.get()+"=================");
    }

    public String getName(){
       return this.name.get();
    }

    public void setName(String str){
        this.name.set(str);
    }
}
